package com.jiangyao.controller;


import com.jiangyao.common.ApiResult;
import com.jiangyao.model.dto.backend.ClassUserAddDTO;
import com.jiangyao.model.dto.backend.ClassUserDTO;
import com.jiangyao.model.dto.backend.ClassUserListPageDTO;
import com.jiangyao.model.vo.backend.ClassUserDetailVO;
import com.jiangyao.service.ClassUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 班级成员信息 前端控制器
 * </p>
 *
 * @author yangyongchuan
 * @since 2020-08-15
 */
@RestController
@RequestMapping("/classUser")
@Api(tags = "组织班级管理-成员管理")
public class ClassUserController extends BaseController {

	@Autowired
	private ClassUserService classUserService;

	/**
	 * 下载成员导入模板
	 *
	 * @param response response
	 */
	@ApiOperation(value = "下载成员导入模板", notes = "")
	@GetMapping("/downloadImportTemplate")
	public void downloadImportTemplate(HttpServletResponse response) {
		classUserService.downloadImportTemplate(response);
	}

	/**
	 * 导入成员
	 *
	 * @param file 上传的文件
	 */
	@ApiOperation(value = "导入成员：组织、班级导入成员")
	@ApiImplicitParam(name = "file", value = "上传的文件", required = true)
	@PostMapping(value = "/importUser")
	@ResponseBody
	public ApiResult<String> importUser(MultipartFile file) {
		if (file == null) {
			return ApiResult.error("文件不能为空");
		}
		classUserService.importUser(file);
		return new ApiResult<>();
	}


	/**
	 * 获取成员列表
	 *
	 * @param listPageDTO 列表DTO
	 * @return 结果
	 */
	@ApiOperation(value = "获取成员列表")
	@PostMapping("/list")
	public ApiResult<ClassUserDetailVO> getListPage(@Validated({ClassUserListPageDTO.ListPage.class}) @RequestBody ClassUserListPageDTO listPageDTO) {
		return ApiResult.success(classUserService.getListPage(listPageDTO));
	}

	/**
	 * 编辑状态（启用、移除）
	 *
	 * @param classUserDTO 编辑状态DTO
	 * @return 结果
	 */
	@ApiOperation(value = "编辑成员状态（启用、禁用）", notes = "必填字段：classUserIdList、status")
	@PostMapping("/editStatus")
	public ApiResult<String> editStatus(@Validated({ClassUserDTO.Delete.class}) @RequestBody ClassUserDTO classUserDTO) {
		classUserService.editStatus(classUserDTO);
		return new ApiResult<>("ok");
	}

	/**
	 * 添加成员
	 *
	 * @param classUserAddDTO 添加成员DTO
	 * @return 结果
	 */
	@ApiOperation(value = "添加成员(原理：从组织成员里面批量添加成员到班级里面)")
	@PostMapping("/addUser")
	public ApiResult<String> addUser(@Validated({ClassUserAddDTO.Add.class}) @RequestBody ClassUserAddDTO classUserAddDTO) {
		classUserService.addUser(classUserAddDTO);
		return new ApiResult<>("ok");
	}
}

